package com.dhf.framework.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.system.entity.SysDictData;
import com.dhf.framework.system.entity.SysDictType;
import com.dhf.framework.system.service.SysDictDataService;
import com.dhf.framework.system.service.SysDictTypeService;

 /**
 * </b>SysDictDataController<br>数据字典数据表
 * @author minstar
 * @since2017年02月22日 21时26分09秒 星期三 
 * @version:1.0
 */
@Controller
@RequestMapping("/system/sysDictData")
public class SysDictDataController extends BaseController{
		private static Logger LOGGER = LoggerFactory.getLogger(SysDictDataController.class);
	
  		@Autowired
  		private SysDictDataService sysDictDataService;
  		
  		@Autowired
  		private SysDictTypeService sysDictTypeService;
  		
  		/**
		  * 
		  * @Title: queryPageList
		  * @Description: 列表页面
		  * @param @param searchEntity
		  * @param @param pagination
		  * @return ModelAndView    返回类型
		  * @throws
		*/
		@RequestMapping(value = "/queryPageList")
		public ModelAndView queryPageList(SysDictData sysDictData, Pagination<SysDictData> pagination) {
			LOGGER.debug(SysDictDataController.class.getName()+":开始查询分页数据！");
			ModelAndView mv = new ModelAndView( "system/sysDictData/sysDictDataList" );
			pagination = this.sysDictDataService.queryPageList( sysDictData, pagination );
			List<SysDictData> dataList=pagination.getItems();
			for(SysDictData entity:dataList){
				SysDictType sysDictType=new SysDictType();
				sysDictType.setTypeId(entity.getTypeId()); 
				List<SysDictType> typeList=this.sysDictTypeService.query(sysDictType);
				if(!typeList.isEmpty()){
					SysDictType resultType=typeList.get(0);
					entity.setTypeCode(resultType.getTypeCode());
					entity.setTypeName(resultType.getTypeName());
				}
			}
			
			
			List<SysDictType> list=this.sysDictTypeService.query(null);
			mv.addObject( "list", list );
			mv.addObject( "pagination", pagination );
			return mv;
		}
		
		
		/**
		 * 
		 * @Title: toMerge
		 * @Description: 新增或修改页面
		 * @param @param entity
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/toMerge")
		public ModelAndView toMerge(SysDictData entity){
			ModelAndView mv = new ModelAndView("system/sysDictData/sysDictDataMerge");
			if(entity.getDataId()!= null){
				SysDictData sysDictData =  this.sysDictDataService.get( entity.getDataId() ) ;
				mv.addObject("sysDictData",sysDictData);
			}else{
				mv.addObject("sysDictData", new SysDictData());
			}
			List<SysDictType> list=this.sysDictTypeService.query(null);
			mv.addObject( "list", list );
			return mv;
		}

		/**
		 * 
		* @Title: doMerge
		* @Description: 新增或修改数据 
		* @param @param request
		* @param @param entity
		* @return ModelAndView    返回类型
		* @throws
		 */
		@RequestMapping(value = "/doMerge")
		public ModelAndView doMerge(HttpServletRequest request, SysDictData entity,RedirectAttributes redirectAttributes) {
			if (entity.getDataId()!=null) {
				LOGGER.debug(SysDictDataController.class.getName()+":开始更新数据！");
				this.addMessage(redirectAttributes, "修改数据成功");
				this.sysDictDataService.update( entity );
			} else {
				LOGGER.debug(SysDictDataController.class.getName()+":开始新增数据！");
				this.sysDictDataService.add( entity );
				this.addMessage(redirectAttributes, "新增据成功");
			}
			return new ModelAndView( "redirect:/system/sysDictData/queryPageList.do" );
		}

		/**
		 * 
		 * @Title: delete
		 * @Description:删除数据
		 * @param @param id
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/delete")
		public ModelAndView delete(Long dataId,RedirectAttributes redirectAttributes) {
			LOGGER.debug(SysDictDataController.class.getName()+":开始删除数据！");
	        this.sysDictDataService.delete( dataId );
	        this.addMessage(redirectAttributes, "删除数据成功");
	        return new ModelAndView( "redirect:/system/sysDictData/queryPageList.do" );
		}
		
		/**
		 * 
		* @Description: 根据字典类型编码typeCode查询typeId，再查询该编码下所有数据集
		* @param sysDictType
		* @param @return    设定文件
		* @return List<SysDictType>    返回类型
		 */
		@RequestMapping(value = "/query")
		@ResponseBody
		public List<SysDictData> query(String typeCode){
			return this.sysDictDataService.queryByTypeCode(typeCode);
		}
}

